using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Service.Platform.RoadData;
using HuiLian.Service.Platform.RoadData.Input;
using HuiLian.Common.Configs;
using HuiLian.Common.Helpers;
using HuiLian.Web.Attributes;
using Microsoft.AspNetCore.Http;
using System.Data;
using System;
using HuiLian.Common.Auth;
using Microsoft.Extensions.Options;

namespace HuiLian.Web.Controllers.Platform
{
    /// <summary>
    /// 道路中心线管理
    /// </summary>
    public class RoadDataController : AreaController
    {
        private readonly IUser _user;
        private readonly UploadConfig _uploadConfig;
        private readonly UploadHelper _uploadHelper;
        private readonly ExcelHelper _excelHelper;
        private readonly IRoadDataService _roadDataServices;

        public RoadDataController(
            IUser user,
            IOptionsMonitor<UploadConfig> uploadConfig,
            UploadHelper uploadHelper,
            ExcelHelper excelHelper, 
            IRoadDataService roadDataServices)
        {
            _user = user;
            _uploadConfig = uploadConfig.CurrentValue;
            _uploadHelper = uploadHelper;
            _excelHelper = excelHelper;
            _roadDataServices = roadDataServices;
        }

        /// <summary>
        /// 查询单条道路中心线
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IResponseOutput> Get(long id)
        {
            return await _roadDataServices.GetAsync(id);
        }

        /// <summary>
        /// 查询分页道路中心线
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> GetPage(PageInput<RoadDataEntity> model)
        {
            return await _roadDataServices.PageAsync(model);
        }

        /// <summary>
        /// 新增道路中心线
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> Add(RoadDataAddInput input)
        {
            return await _roadDataServices.AddAsync(input);
        }

        /// <summary>
        /// 修改道路中心线
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResponseOutput> Update(RoadDataUpdateInput input)
        {
            return await _roadDataServices.UpdateAsync(input);
        }

        /// <summary>
        /// 删除道路中心线
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IResponseOutput> SoftDelete(long id)
        {
            return await _roadDataServices.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量删除道路中心线
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResponseOutput> BatchSoftDelete(long[] ids)
        {
            return await _roadDataServices.BatchSoftDeleteAsync(ids);
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Login]
        public async Task<IResponseOutput> ExcelImport(IFormFile file)
        {
            try
            {
                var config = _uploadConfig.Document;
                var res = await _uploadHelper.UploadAsync(file, config, new { _user.Id });
                if (res.Success)
                {
                    _excelHelper.FilePath = @$"{config.UploadPath}\{res.Data.FileRelativePath}";
                    _excelHelper.ToDataTable();
                    DataTable dt = _excelHelper._dataTable;
                    var list = ExtendHelper.GetList<RoadDataEntity>(dt);

                    return await _roadDataServices.AddListAsync(list);
                }

                return ResponseOutput.NotOk("上传失败！");
            }
            catch (Exception ex) { return ResponseOutput.NotOk(ex.Message); }
        }
    }
}
